from pyE17 import io
import matplotlib.pyplot as plt
import ptycho2 as ptycho
import numpy as np
import sys


#h5 = io.h5read(filename)
#img = h5['eh5']['images'][0]
#plt.figure()
#plt.imshow(img)
#plt.show()

def prep_eiger_file(base_path,user,dpsize,ctr,detector_distance,first_scan,last_scan,step_size_y,sx,nx,dat_file):
    # Need to "hack" the defaults for continuous scans
#     ptycho.cSAXS.FILENAME_PATTERN = '{read_path}/{prefix}{scan_number:05d}_00000_{index}.{file_extension}'
    ptycho.cSAXS.FILENAME_MULTEXP_PATTERN = '{read_path}/{prefix}{scan_number:05d}_{index}_00000.{file_extension}'
    ptycho.cSAXS.FILENAME_PATTERN = '{read_path}/{prefix}{scan_number:05d}_{index}_00000.{file_extension}'
    ptycho.cSAXS.READ_PATH_PATTERN = '{base_path}/S{scan_number:05d}'
    # Empty parameter structure
    p = ptycho.Param()

    p.verbose_level = 3
    p.interactive = False                         # Not used for now
    p.base_path = base_path
    p.user = user
    #p.scan_number = 300
    p.dpsize = dpsize                          # Crop size (used to be "asize")
    p.ctr = ctr                               # Center of cropped array
    #p.motors = ('py', 'px')                      # Motor names in y and x
    #p.motors_multiplier = 1e-3                    # Multiplier to convert motor values to meters
    p.motors = None                               # Motor names in y and x
    p.energy = 6.2                                # Energy in keV
    p.detector_pixel_size = 75e-6                # Detector pixel size
    p.detector_distance = detector_distance                    # Detector distance
    p.scan_type = 'raster'                            # None (use spec), or 'raster', 'round', 'round_ROI'
    p.scan_params.nx = nx
    p.scan_params.ny = 0
    p.scan_params.step_size_x = sx
    p.scan_params.step_size_y = step_size_y
#     p.spec_filename = ptycho.cSAXS.spec.SpecInfo(base_path + '/specES1/dat-files/'+dat_file)
    p.spec_filename = ptycho.cSAXS.spec.SpecInfo(base_path + '/'+dat_file)
    p.pilatus_dir = 'eiger'
#     p.pilatus_mask = base_path + '/matlab/ptycho/eiger_valid_mask.mat'
    p.pilatus_mask = base_path + '/eiger_valid_mask.mat'
    p.isEigerFile = True
#    specinfo = ptycho.cSAXS.spec.SpecInfo(base_path + '/specES1/dat-files/'+dat_file)

    for kk in range(first_scan,last_scan+1):
        p.scan_number = kk
        ptycho.cSAXS.prepare_data(p)

    scan_label = 'S%05d_S%05d' % (first_scan, last_scan)

    fpattern = base_path + '/analysis/S%05d/S%05d_data_' + '%dx%d.h5' % dpsize

    # Load all data as dataScan objects
    ds_all = [ptycho.experiment.DataScan(fpattern % (i,i)) for i in range(first_scan, last_scan+1)]

    # The "out" scan will be based on one of these
    ds = ptycho.experiment.DataScan(fpattern % (first_scan,first_scan))

    # Now modify the structure
    # First combine data
    ds.data = np.concatenate([di.data for di in ds_all])
    ds.scan_info.shape = ds.data.shape
    ds.scan_info.scan_label = scan_label

    # Edit positions
    positions = np.concatenate([ds_all[i].scan_info.positions_theory + [0., i*step_size_y] for i in range(len(ds_all))])[:,-1::-1]
    ds.scan_info.positions = positions
    ds.scan_info.positions_theory = positions

    ds.scan_info.data_filename = base_path + '/analysis/%s/%s_data_' % (scan_label, scan_label) + '%dx%d.h5' % dpsize

    # little bug that needs to be solved7.18
    ds.mask = ds.fmask
    ds.save()

user = 'e14438'
dpsize = (230,230)
ctr=(278, 373)
detector_distance= 7.2122
first_scan = 9187
last_scan = 9207
# base_path = '/afs/psi.ch/user/b/braun_p/data'
base_path = '/space/ptychography'
#base_path = '/home/philipp/Documents/master'
dat_file = 'specES1_started_2013_03_20_1108.dat'
scans = [(9187,9207,250.000,20),
(9208,9228,250.000,20),
(9229,9249,250.000,20),
(9250,9270,250.000,20),
(9271,9291,250.000,20),
(9292,9312,250.000,20),
(9313,9333,250.000,20),
(9334,9354,250.000,20),
(9355,9375,250.000,20),
(9376,9396,250.000,20),
(9397,9417,250.000,20),
(9418,9438,250.000,20),
(9441,9453,416.667,12),
(9454,9466,416.667,12),
(9467,9479,416.667,12),
(9480,9492,416.667,12),
(9493,9505,416.667,12),
(9506,9518,416.667,12),
(9519,9531,416.667,12),
(9532,9544,416.667,12),
(9545,9557,416.667,12),
(9558,9570,416.667,12),
(9571,9583,416.667,12),
(9584,9596,416.667,12),
(9599,9606,714.286,7),
(9607,9614,714.286,7),
(9615,9622,714.286,7),
(9623,9630,714.286,7),
(9631,9638,714.286,7),
(9639,9646,714.286,7),
(9647,9654,714.286,7),
(9655,9662,714.286,7),
(9663,9670,714.286,7),
(9671,9678,714.286,7),
(9681,9686,1000.000,5),
(9687,9692,1000.000,5),
(9693,9698,1000.000,5),
(9699,9704,1000.000,5),
(9705,9710,1000.000,5),
(9711,9716,1000.000,5),
(9717,9722,1000.000,5),
(9723,9728,1000.000,5),
(9729,9734,1000.000,5),
(9735,9740,1000.000,5),
(9743,9746,1666.667,3),
(9747,9750,1666.667,3),
(9751,9754,1666.667,3),
(9755,9758,1666.667,3),
(9759,9762,1666.667,3),
(9763,9766,1666.667,3),
(9767,9770,1666.667,3),
(9771,9774,1666.667,3),
(9775,9778,1666.667,3),
(9779,9782,1666.667,3)]

scans2 = [(12710,30),(12741,30)]
# (9783,49),
# (9833,49),
# (9883,39),
# (9923,39),
# (9963,29),
# (9993,29),
# (10023,19),
# (10043,19),
# (10063,9),
# (10073,9 ),
# (10083,4),
# (10088,4),
# (10093,2),
# (10096,2),
# (10099,49),
# (10149,49),
# (10199,39),
# (10239,39),
# (10279,29),
# (10309,29),
# (10339,19),
# (10359,19),
# (10379,9),
# (10389,9),
# (10399,4),
# (10404,4),
# (10409,2),
# (10412,2),
# (10415,49),
# (10465,49),
# (10515,39),
# (10555,39),
# (10595,29),
# (10625,29),
# (10655,19),
# (10675,19),
# (10695,9),
# (10705,9),
# (10715,4),
# (10720,4),
# (10725,2),
# (10728,2),
# (10731,49),
# (10781,49),
# (10831,39),
# (10871,39),
# (10911,29),
# (10941,29),
# (10971,19),
# (10991,19),
# (11011,9),
# (11021,9),
# (11031,4),
# (11036,4),
# (11041,2),
# (11044,2),
# (11047,49),
# (11097,49),
# (11147,39),
# (11187,39),
# (11227,29),
# (11257,29),
# (11287,19),
# (11307,19),
# (11327,9),
# (11337,9),
# (11347,4),
# (11352,4),
# (11357,2),
# (11360,2),
# (11363,49),
# (11413,49),
# (11463,39),
# (11503,39),
# (11543,29),
# (11573,29),
# (11603,19),
# (11623,19),
# (11643,9),
# (11653,9),
# (11663,4),
# (11668,4),
# (11673,2),
# (11676,2),
# (11679,49),
# (11729,49),
# (11779,39),
# (11819,39),
# (11859,29),
# (11889,29),
# (11919,19),
# (11939,19),
# (11959,9),
# (11969,9),
# (11979,4),
# (11984,4),
# (11989,2),
# (11992,2),
# (11995,49),
# (12045,49),
# (12095,39)]

size = 5e-6

#for scan in scans:
#    print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
#    print 'Preparing scans %d_%d' % (scan[0],scan[1])
#    print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
#    prep_eiger_file(base_path, user, dpsize, ctr, detector_distance, scan[0], scan[1], size/scan[3],size/(scan[3]+1),scan[3],dat_file)
#


for scan in scans2:
    print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
    print 'Preparing scans %d_%d' % (scan[0],scan[0]+scan[1])
    print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
    prep_eiger_file(base_path, user, dpsize, ctr, detector_distance, scan[0], scan[0]+scan[1], size/scan[1],size/(scan[1]+1),scan[1],dat_file)
#
#contdmesh_string = 'cont_dmesh px %0.4f %0.4f %d py %0.4f %0.4f %d %0.4f %0.5f'
#
#
#for scan in scans:
#    s = contdmesh_string % (-size/2*1e3,size/2*1e3,scan[3],size/2*1e3,size/2*1e3,scan[3], exp_t,p.readout_time)
#    string =  'Scans %d_%d: ' + s + ' +++ stepsize = %03.03f nm, speed = %03.03f nm/s'
#




